package com.tjsiqi.controller;

import com.tjsiqi.Vo.WebUserInfo;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.SysUser;
import com.tjsiqi.model.Webusercontrol;
import com.tjsiqi.param.DeptParam;
import com.tjsiqi.param.SearchJbzInfoParam;
import com.tjsiqi.param.UserParam;
import com.tjsiqi.service.SysDeptService;
import com.tjsiqi.service.SysUserService;
import com.tjsiqi.service.WebUserControlService;
import com.tjsiqi.util.DateTimeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/sys/user")
public class WebUserController {
    @Resource
    SysUserService sysUserService;

    @Resource
    SysDeptService sysDeptService;

    @Resource
    WebUserControlService webUserControlService;

    //搅拌站列表页面
    @RequestMapping("/JbzInfoM.page")
    public ModelAndView  webUserInfoPage(){
        return  new ModelAndView("JbzInfoM");
    }

    @RequestMapping("/userInfo.page")
    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String path = "userInfo.jsp";
        response.sendRedirect(path);
    }
// 查询搅拌站用户信息
    @RequestMapping("/getUserInfo.json")
    @ResponseBody
    public void getUserInfo(HttpServletRequest request, HttpServletResponse response,@RequestParam(value="jbzCode",required = false) String jbzCode, @RequestParam("userId") int userId)throws IOException, ServletException {
        //管理员不允许在用户界面修改
        if(jbzCode.equals("10000") || jbzCode.isEmpty()){
            response.sendRedirect("/sys/dept/dept.page");
            return ;
        }
        //用户修改信息或管理员修改用户信息。
        WebUserInfo webUserInfo = new WebUserInfo();
        SysUser sysUser = sysUserService.getUserInfo(jbzCode,userId);
        webUserInfo.setId(sysUser.getId());
        webUserInfo.setUsername(sysUser.getUsername());
        webUserInfo.setTelephone(sysUser.getTelephone());
        webUserInfo.setMail(sysUser.getMail());
        webUserInfo.setRemark(sysUser.getRemark());
        webUserInfo.setDeptId(sysUser.getDeptId());
        if(jbzCode !=null && jbzCode != "") {
            Webusercontrol webusercontrol= webUserControlService.getJbzInfo(jbzCode);
            if(webusercontrol != null ) {
                webUserInfo.setMc(webusercontrol.getMc());
                webUserInfo.setCode(webusercontrol.getCode());
                webUserInfo.setDz(webusercontrol.getDz());
                webUserInfo.setDh(webusercontrol.getDh());
                webUserInfo.setFzr(webusercontrol.getFzr());
                webUserInfo.setProvince(webusercontrol.getProvince());
                webUserInfo.setCity(webusercontrol.getCity());
                webUserInfo.setDistrict(webusercontrol.getDistrict());
                webUserInfo.setLx(webusercontrol.getLx());
                webUserInfo.setBz(webusercontrol.getBz());
            }
        }
        System.out.println(webUserInfo.getProvince()+"shi"+webUserInfo.getCity()+"qu"+webUserInfo.getDistrict());
        request.getSession().setAttribute("webUserInfo",webUserInfo);
        response.sendRedirect("/zhuce/userInfo.page");
        return ;
    }

//  更新搅拌站信息
    @RequestMapping("/updateWebUser")
    @ResponseBody
    public JsonData updateWebUser (SysUser param, Webusercontrol webusercontrol, @RequestParam(value="xlx",required=false) String[] xlx ) {

        int lx  = 0;
        String lxlx = "";
        if(xlx != null){
            for (int i = xlx.length-1;i>-1;i--){
                lxlx = lxlx+xlx[i];
                try {
                    lx= Integer.valueOf(lxlx);
                }catch(NumberFormatException e){
                    e.printStackTrace();
                }catch (IllegalArgumentException e){
                    e.printStackTrace();
                }
            }}
        webusercontrol.setLx(lx);
        param.setJbzName(webusercontrol.getMc());
        DeptParam deptParam = new DeptParam();
        if(param.getId() != null) {
            deptParam.setId(param.getDeptId());
        }
        deptParam.setName(webusercontrol.getMc());
        if(webusercontrol.getProvince().equals("天津市")){
            deptParam.setParentId(2);
        }else if(webusercontrol.getProvince().equals("北京市")){
            deptParam.setParentId(3);
        }else {
            deptParam.setParentId(4);
        }
        deptParam.setSeq(1);
       if(deptParam != null&&webusercontrol != null && param != null&&param.getId()!=null && deptParam.getId() !=null)
        {
            try {
                sysDeptService.update(deptParam);
                sysUserService.updateById(param);
                webUserControlService.update(webusercontrol);
            } catch (Exception e){
                e.printStackTrace();
                return  JsonData.fail(e.toString());
            }

        }else{
            return JsonData.fail("更新失败，请确保信息完整，或检查网络是否有问题，重新登录并重试");
        }
        return JsonData.success();
    }

    //管理员查询搅拌站列表
    @RequestMapping("/JbzInfoM.json")
    @ResponseBody
    public JsonData getJbzInfoMs(SearchJbzInfoParam searchJbzInfoParam, PageQuery page)
    {
        return JsonData.success(webUserControlService.getJbzInfoMs(searchJbzInfoParam,page));
    }


    //搅拌站续期
    @RequestMapping("/updateJbzByCode")
    @ResponseBody
    public JsonData updateJbzByCode(String code)
    {
        Webusercontrol webusercontrol = new Webusercontrol();
        webusercontrol.setCode(code);
        webusercontrol.setModifytime(new Date());
        return JsonData.success(webUserControlService.updateJbzByCode(webusercontrol));
    }

    //搅拌站停止
    @RequestMapping("/stopJbzByCode")
    @ResponseBody
    public JsonData stopJbzByCode(String code)
    {
        Webusercontrol webusercontrol = new Webusercontrol();
        webusercontrol.setCode(code);
        webusercontrol.setModifytime(DateTimeUtil.strToDate("2018-01-01 00:00:00"));
        return JsonData.success(webUserControlService.updateJbzByCode(webusercontrol));
    }


}
